/*
 *  Copyright (C) 2011  social-tourist.org
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.socialtourist.web.client.widgets;

import static org.socialtourist.web.client.SocialTourist.constants;
import static org.socialtourist.web.client.SocialTourist.imageBundle;

import org.socialtourist.web.client.SocialTouristServiceAsync;
import org.socialtourist.web.client.presenter.ProjectNeedsPresenter;
import org.socialtourist.web.shared.data.SocialProject;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.StackPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ProjectDetailsPanel extends PopupPanel 
{
	//private final PopupPanel popupPanel;
	private final Button closeButton;
	private final StackPanel stackPanel;
	private final Label nameLabel = new Label();
	private final Label descriptionLabel = new Label();
	private final Label locationLabel = new Label("Hier Lagebeschreibung, ggf. zukuenftig GoogleMaps Widget oder aehnliches.");
	private final ProjectNeedsPresenter needsPresenter;
	
	private SocialProject project;
	private SocialTouristServiceAsync service;
	private Image image1;
	private Image image2;

	public ProjectDetailsPanel(SocialTouristServiceAsync service) {
		
		//popupPanel = new PopupPanel();
		this.setGlassEnabled(true);
		this.setAnimationEnabled(true);
		this.setStylePrimaryName("projectPopup");
		this.service = service;
		
		nameLabel.setStyleName("titleLabel");
		
		// FIXME wrongly directed dependency
		this.needsPresenter = new ProjectNeedsPresenter(service);
				
		// We can set the id of a widget by accessing its Element
		closeButton = new Button("Close");
		closeButton.getElement().setId("closeButton");
		// Add a handler to close the DialogBox
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				hide();
			}
		});
		
		HorizontalPanel outerHPanel = new HorizontalPanel();
		HorizontalPanel hPanel = new HorizontalPanel();
		
		image1 = new Image();
		image1.setWidth("320px");
		image1.setHeight("256px");
		hPanel.add(image1);
		image2 = new Image();
		image2.setWidth("320px");
		image2.setHeight("256px");
		hPanel.add(image2);
		
		VerticalPanel vPanel = new VerticalPanel();
		vPanel.add(nameLabel);
		//vPanel.setWidth("800px");
		vPanel.addStyleName("dialogVPanel");
		vPanel.setHorizontalAlignment(VerticalPanel.ALIGN_LEFT);
		
		stackPanel = new StackPanel();
		stackPanel.add(descriptionLabel, constants.project_description(), false);
		stackPanel.add(locationLabel, constants.project_location(), false);
		stackPanel.add(needsPresenter.getWidget(), constants.project_needs(), false);
		stackPanel.setWidth("640px");
		//stackPanel.add(, constants.project_needs(), false);
		vPanel.add(hPanel);
		vPanel.add(stackPanel);
		vPanel.add(closeButton);
		
		outerHPanel.add(vPanel);
		//outerHPanel.add(new Label("Hier Bedarf"));
		
		this.setWidget(vPanel);
	}
	
	public void center() {
		assert(project != null);
		super.center();
	}
	
	public void hide(){
		super.hide();
	}
	
	public void setProject(SocialProject project)
	{
		this.project = project;
		this.needsPresenter.setProject(project);
		updateFields();
	}

	private void updateFields() {
		descriptionLabel.setText(project.getDescription());
		locationLabel.setText(project.getLocationString());
		nameLabel.setText(project.getName());
		
		if (project.getImage1() != null)
		{
			image1.setUrl("/socialtourist/image?id=" + project.getImage1());
		}
			
		if (project.getImage2() != null)
		{
			image2.setUrl("/socialtourist/image?id=" + project.getImage2());
		}
		
		needsPresenter.setProject(project);
	}

	
	
}
